// pages/lucky/index.js
import { saveImageToPhotosAlbum,downloadFile,showModal} from '../../utils/asyncwx.js'
const http = require('../../utils/http.js');
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tname:null,
    uid:null,
    tfeed:null,
    imgurl:null,
    jianjie_txt:null,
    showbutton:false,
    tpayed:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
    //转义方法
    escape2Html(str) {
      var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
      return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; }).replace('<section', '<div').replace('<img', '<img style="max-width:100%;height:auto" ');
    },
  async loadnews() {
    //从后台读取首页信息
    let url = 'topnews/index'
    let data = {
      id: 2
     
    }
    let res= await http.post(url,data)
   
    this.setData({       
      jianjie_txt: this.escape2Html(res.jianjieTxt)
     })
  },//end of loadnews
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  async savepic(){
    //保存图片
    //console.log(this.data.imgurl)
    let res=await downloadFile(this.data.imgurl)
   
     let res0= await saveImageToPhotosAlbum(res.tempFilePath).catch(err=>{
      
      
     })
     //console.log(res0) //errMsg: "saveImageToPhotosAlbum:ok"
     if (res0.errMsg === "saveImageToPhotosAlbum:ok") {
      //保存成功
      await showModal({content:'保存成功!',showCancel:false})
      let showbutton=false
      this.setData({
        showbutton
      })
     }
     if (res0.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
        console.log("当初用户拒绝，再次发起授权")
        await showModal({content:'请授权保存图片!',showCancel:false})
        let showbutton=true
        this.setData({
          showbutton
        })
    }
  },
  openSetting() {  
    wx.openSetting()
    let showbutton=false
    this.setData({
      showbutton
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadnews()
     const uid = wx.getStorageSync('uid')
     const userInfo=wx.getStorageSync("userInfo")
     //const tname = wx.getStorageSync('tname')
     this.setData({
      uid
     })
     if(userInfo.avatarUrl){
      //有授权的用户信息，调取个人信息
      this.get_data()    
     

    }
    
  },
  async get_data() {
    //获取信息    
     const uid=this.data.uid
     if (uid){
       let url = 'Client/getbalance/';
       let data = {
         uid: this.data.uid,
       }
       let res= await http.post(url,data)
      
      
         if (res.client.name!=null ) {
           //有名字
           let imgurl=null
          let tfeed=res.client.tfeed
          let tpayed=res.client.tpayed
          let status="通过"
          
          if (res.client.status===0){
           status="审核中"
           tfeed=0
          }
          if (res.client.status===1){
           status="通过"
            imgurl=res.client.luckyid
            imgurl='https://buck-cct.oss-cn-shenzhen.aliyuncs.com/lucky/'+imgurl+'.jpg'
          }
          if (res.client.status===2){
            status="未通过"
            tfeed=0
           
          }
          wx.setStorageSync('tname',res.client.name)
          this.setData({         
            tname: res.client.name,
            tdays: res.client.tcount,          
            status:status,
            tfeed,
            imgurl,
            tpayed
           
  
          })
        }
      
      
     
     }
 
     
 
 
   },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})